<?php

/**
 * 广告平台
 *
 * @author Ciro <ciro.han@shangbox.com>
 * @copyright 2011-2012 shangbox.com
 * $Id: App.php 162 2012-11-09 04:04:30Z Jun.Lu.726@gmail.com $
 * @history 2011-2012  ::  Ciro  ::  Create File
 */
class Controller_App extends Controller_Abstract
{

    public function init()
    {
        parent::init();
        $menuLeft = include (RESOURCE_PATH . 'app.conf.php');
        $this->assign("menuLeft", $menuLeft);
        $this->data = array(array("title" => "应用管理", "url" => "/app"));
    }

    /**
     * 首页 获取应用列表
     */
    public function indexAction()
    {
        include (RESOURCE_PATH . 'appSource.conf.php');
        $this->data[] = array("title" => "应用列表", "url" => "#");
        $this->navigationSet($this->data);
        $app = Model("App");
        //搜索
        $date = $this->getPost("seach");
        $p = $this->get("p") ? $this->get("p") : 1;

        $offset = 10;
        $sum = $offset * ($p - 1);
        $data = $app->_getAppList($date, $sum, $offset, $date, 1);
        if (empty($data[0])) {
            $count = 0;
        } else {
            $count = $data[0];
        }
        $data = $data[1];
        if (is_array($data)) {
            foreach ($data as $k => $v) {
                $data[$k]["status"] = $runType[$v["status"]];
                $data[$k]["ctim"] = date("Y-m-d H:i:s", $data[$k]["ctim"]);
            }
        }
        //分页读取       
        $params = array(
            'total_rows' => $count, #(必须) 总条数
            'method' => 'default', #(必须)显示方式  default为php 
            'parameter' => 'p=?', #(必须) 显示方式 get传值
            'now_page' => $p, #(必须) 页码
            'list_rows' => $offset, #(可选) 默认为15 显示数量
        );
        $page = new Com_PageList($params);
        $pageStr = $page->show(1);
        $this->assign("runType", $runType);
        $this->assign("seach", $date);
        $this->assign("appData", $data);
        $this->assign("page", $pageStr);
    }

    /**
     * 广告管理
     */
    public function adlistAction()
    {
        $this->data[] = array("title" => "广告管理", "url" => "#");
        $this->navigationSet($this->data);
    }

    /**
     * 应用禁止与解除
     */
    public function controlAction()
    {
        $this->data[] = array("title" => "应用禁止/解除", "url" => "#");
        $this->navigationSet($this->data);
    }

    /**
     * 应用报表管理
     */
    public function reportAction()
    {
        $this->data[] = array("title" => "应用报表", "url" => "#");
        $this->navigationSet($this->data);
        include (RESOURCE_PATH . 'appSource.conf.php');
        $app = Model("App");
        //搜索
        $date = $this->getPost("seach");
        $p = $this->get("p") ? $this->get("p") : 1;

        $offset = 10;
        $sum = $offset * ($p - 1);
        $data = $app->_getAppList($date, $sum, $offset, $date, 3);
        if (empty($data[0])) {
            $count = 0;
        } else {
            $count = $data[0];
        }
        $data = $data[1];
        if (is_array($data)) {
            foreach ($data as $k => $v) {
                $data[$k]["status"] = $runType[$v["status"]];
                $data[$k]["ctim"] = date("Y-m-d H:i:s", $data[$k]["ctim"]);
            }
        }
        //分页读取       
        $params = array(
            'total_rows' => $count, #(必须) 总条数
            'method' => 'default', #(必须)显示方式  default为php 
            'parameter' => 'p=?', #(必须) 显示方式 get传值
            'now_page' => 1, #(必须) 页码
            'list_rows' => $offset, #(可选) 默认为15 显示数量
        );
        $page = new Com_PageList($params);
        $pageStr = $page->show(1);
        $this->assign("seach", $date);
        $this->assign("appData", $data);
        $this->assign("page", $pageStr);
    }

    /**
     * 应用列表
     */
    public function appAction()
    {
        $this->data[] = array("title" => "应用列表", "url" => "#");
        $this->navigationSet($this->data);
    }

    public function showAction()
    {
        $this->data[] = array("title" => "应用列表", "url" => "/app");
        $this->data[] = array("title" => "应用详细", "url" => "#");
        $this->navigationSet($this->data);

        $id = $this->get("id");
        if ($id < 0) {
            redirect(WEB_DOMAIN . "/app");
        }
        $app = Model("App");
        $data = $app->_getApp($id); //应用广告添加
        $this->assign("data", $data);
        //分类读取
        $appClass = $this->disposeClass($app->_getClass());
        $this->assign("appClass", $appClass);
    }

    /**
     * 开发者应用添加
     */
    public function addAction()
    {
        $this->data[] = array("title" => "应用添加", "url" => "#");
        $this->navigationSet($this->data);
        //分类读取 bg_app_class 
        $data = $this->getPost("add");
        $app = Model("App");
        if ($data["id"] > 0) {
            //输入值检查
            $ok = $this->addAppCheck($data);
            if ($ok[0]) {
                if ($app->_session['appID'] > 0) {
                    $ok = $app->_updateApp($app->_session['appID'], $data);
                } else {
                    $ok = $app->_addApp($data["uid"], $data);
                }
                if ($ok) {
                    //设置应用ID
                    $app->_session['appID'] = $ok;
                    redirect(WEB_DOMAIN . "/app/adset");
                } else {
                    //添加失败
                    $this->assign("add", $data);
                }
            } else {
                //
                $this->assign("add", $data);
                $this->assign("err_msg", $ok[1]);
            }
        }
        if (isset($app->_session['appID'])) {
            $data = $app->_getApp($app->_session['appID']);

            $obj = Model("User");
            $username = $obj->getUserInfo($data["uid"], 1);
            $assname = $obj->getUserInfo($data["assid"], 2);
            $data["username"] = $username["email"];
            $data["assname"] = $assname["admin_name"];
            $this->assign("add", $data);
        }
        //分类读取
        $appClass = $this->disposeClass($app->_getClass());
        $this->assign("appClass", $appClass);
    }

    public function adsetAction()
    {
        $this->data[] = array("title" => "应用添加", "url" => "#");
        $this->navigationSet($this->data);
        $data = $this->getPost("add");
        $app = Model("App");
        //应用信息获取
        $datas = $app->_getApp($app->_session['appID']);
        $obj = Model("User");
        $username = $obj->getUserInfo($datas["uid"], 1);
        $assname = $obj->getUserInfo($datas["assid"], 2);
        $data["username"] = $username["email"];
        $data["assname"] = $assname["admin_name"];
        $data["uid"] = $username["id"];
        if (isset($data[0])) {
            $ok = $this->addAppsetCheck($data);
            if ($ok[0]) {
                $ok = $app->_addsetApp($data, 8); //应用广告添加
                if ($ok) {
                    unset($app->_session['appID']);
                    redirect(WEB_DOMAIN . "/app/show/id/{$datas["id"]}");
                } else {
                    $this->assign("add", $data);
                }
            } else {
                $this->assign("add", $data);
                $this->assign("err_msg", $ok[1]);
            }
        }
        //获取应用信息
        $this->assign("add", $data);
    }

    public function editAction()
    {
        $this->data[] = array("title" => "应用编辑", "url" => "#");
        $this->navigationSet($this->data);
    }

    /**
     * 应用删除
     */
    function deleteAction()
    {
        $id = $this->get("id");
        if ($id < 0) {
            redirect(WEB_DOMAIN . "/app");
        }
        $app = Model("App");
        $ok = $app->_delApp($id); //应用广告添加
        if ($ok) {
            redirect(WEB_DOMAIN . "/app");
        }
    }

    /**
     * 应用信息添加审核
     * @param type $data
     * @return type
     */
    function addAppCheck($data)
    {
        $ok = 0;
        $check = new Com_Validate();
        if ($check->notEmpty($data["name"])
                && $check->notEmpty($data["version"])
                && $check->notEmpty($data["appurl"])
                && $check->notEmpty($data["introduce"])
                && $check->notEmpty($data["appimg"]) && $data["classid"] > 0) {
            $ok = 1;
        }
        return array($ok, "填写应用信息有误 或 不完整");
    }

    private function addAppsetCheck($data)
    {
        $ok = 0;
        $check = new Com_Validate();
        $sum = 0;
        for ($i = 1; $i < 9; $i++) {
            if (is_numeric($data[$i]["one"]) && is_numeric($data[$i]["two"]) && $data[$i]["one"] < $data[$i]["two"]) {
                $sum++;
            }
        }
        if ($sum > 7) {
            $ok = 1;
        }
        return array($ok, "填写应用信息有误 或 不完整");
    }

    /**
     * 应用广告排期控制
     */
    public function priorityAction()
    {
        $this->data[] = array("title" => "排期控制", "url" => "#");
        $this->navigationSet($this->data);
    }

    /**
     * 应用广告效果
     */
    public function effectAction()
    {
        $this->data[] = array("title" => "排期情况", "url" => "#");
        $this->navigationSet($this->data);
    }

    public function showWebuserAction()
    {
        $sarch["username"] = $this->getPost("username");
        $obj = Model("User");
        $webuser = $obj->getWebuserList(0, 10, $sarch, 1);
        foreach ($webuser as $vl) {
            //$h = str_ireplace($key, "<strong>".$key."</strong>", $h);
            echo "<option value='" . $vl["id"] . "'>" . $vl["email"] . "</option>";
        }
        return false;
    }

    /**
     * 获取系统用户列表
     * @return boolean
     */
    public function showSysuserAction()
    {
        $sarch["assname"] = $this->getPost("assname");
        $obj = Model("User");
        $sysuser = $obj->getSysuserList(0, 10, $sarch, 1);
        foreach ($sysuser as $vl) {
            //$h = str_ireplace($key, "<strong>".$key."</strong>", $h);
            echo "<option value='" . $vl["admin_id"] . "'>" . $vl["admin_name"] . "</option>";
        }
        return false;
    }

    /**
     * 分类显示列表
     * @param type $data
     * @return type
     */
    function disposeClass($data)
    {
        $disdata = array();
        foreach ($data as $vl) {
            if ($vl["parentid"] == 0) {
                //顶级类目 $disdata
                $disTop[$vl["id"]] = $vl["title"];
            } else {
                $disdata[$vl["id"]] = $vl;
            }
            $disd[$vl["id"]] = $vl["title"];
        }
        foreach ($disdata as $key => $val) {
            $disdata[$key]["title"] = "【" . $disd[$disdata[$key]["parentid"]] . "】" . $disdata[$key]["title"];
        }
        return $disdata;
    }

}

?>
